using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public static Player instance;
    // Start is called before the first frame update
    LevelSelfDownModel ldm = LevelSelfDownModel.Nomal;
    public AudioClip hunt;

    public AudioClip eat;
    private Animator Animator;
    PlayerStatus status = PlayerStatus.Nomal;

    public GameObject coach;
    List<Coach> Coaches = new List<Coach>();

    Camera mainCamera;

    int LevelDownModelRefreshTime = 0;

    int Timer = 0;

    Vector3 initPos;

    public float timeMuteki = 2.0f;
    float mutekiTimer;

    bool isEnding = false;

    private void Awake()
    {
        if (Player.instance == null)
        {
            Player.instance = this;
        }
    }

    void Start()
    {
        mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
        Animator = GetComponent<Animator>();
        initPos = gameObject.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (!Global.instance.StartFlag)
        {
            return;
        }

        if (Global.instance.IsEnding)
        {

            gameObject.transform.Translate(Vector3.right * 2f * Time.deltaTime, Space.World);
        } else
        {

            Vector3 horizental = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);

            if (transform.position.y >= mainCamera.transform.position.y + 4.5)
            {
                horizental.y = -0.5f;
            }
            if (transform.position.y <= mainCamera.transform.position.y - 4.5)
            {
                horizental.y = 0.5f;
            }
            if (transform.position.x < mainCamera.transform.position.x - 9)
            {
                horizental.x = 0.5f;
            }
            if (transform.position.x > mainCamera.transform.position.x + 3)
            {
                horizental.x = -0.5f;
            }

            transform.position = transform.position + horizental * Time.deltaTime * 8;
        }

        

    }




    public void Restart()
    {
        gameObject.transform.position = initPos;
        status = PlayerStatus.Nomal;
        isEnding = false;

        foreach (var coach in Coaches)
        {
            coach.DestroySelf();
            Coaches.Remove(coach);
        }
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("Collision------------>");

        Destroy(collision.gameObject);

        if (status == PlayerStatus.Muteki)
        {
            return;
        }
        switch (collision.tag)
        {
            case "Bullet":
                
                if (GetComponent<AudioSource>().clip != hunt) 
                {
                    GetComponent<AudioSource>().clip = hunt;
                    GetComponent<AudioSource>().Play();
                }
                if (Global.instance.Level > 1)
                    Global.instance.Level--;
                else

                    if (!Global.instance.IsEnding)
                    {

                        EventManager.GetInstance.TriggerEvent("gameOver");
                    }

                status = PlayerStatus.Muteki;
                Animator.SetTrigger("Muteki");
                ChangeLevelDownModal(LevelSelfDownModel.Hited);
                DiscardLatestCoach();
                break;
            case "Food":
                if (GetComponent<AudioSource>().clip != eat)
                {
                    GetComponent<AudioSource>().clip = eat;
                    GetComponent<AudioSource>().Play();
                }
                if (Global.instance.Level < 10)
                    Global.instance.Level++;

                ChangeLevelDownModal(LevelSelfDownModel.Eat);
                TryCreateNewCoach();
                break;
        }
    }

    public void TryCreateNewCoach()
    {
        Debug.Log("Coaches Count: " + Coaches.Count);
        if (Coaches.Count >= 4)
        {
            return;
        }



        if (Coaches.Count == 0)
        {
            GameObject coachGo = Instantiate(coach, transform.position, Quaternion.identity);
            Coach c = coachGo.GetComponent<Coach>();
            c.target = transform;
            Coaches.Add(c);
        }
        else
        {
            int index = Coaches.Count - 1;
            GameObject coachGo = Instantiate(coach, Coaches[index].transform.position, Quaternion.identity);
            Coach c = coachGo.GetComponent<Coach>();
            c.target = Coaches[index].transform;
            Coaches.Add(c);
        }
    }

    public void DiscardLatestCoach()
    {
        if (Coaches.Count <= 0)
        {
            return;
        }
        var latestCoach = Coaches[Coaches.Count - 1];
        Debug.Log("Latest : " + latestCoach);
        latestCoach.DestroySelf();
        Coaches.Remove(latestCoach);
    }

    void FixedUpdate()
    {
        
        if (Timer % 5 == 0)
        {
            float downpos = (float)((int)ldm * 0.02);
            if (downpos > 0)
            {
                Background.GetInstance.GroundMoveUp(downpos);
            }
            else
            {
                Background.GetInstance.GroundMoveDown((-1 * downpos));
            }
            //if (/*backGrouundl.transform.position.y < 8.5*/Background.GetInstance.GetTransPos().y < 8.6)
            //{
            //    if (downpos > 0)
            //    {
            //        Background.GetInstance.GroundMoveUp(downpos);
            //    }
            //    else
            //    {
            //        Background.GetInstance.GroundMoveDown((-1 * downpos));
            //    }
            //}
            //else
            //{
            //    transform.position = transform.position + new Vector3(0, -1 * downpos, 0);
            //}
        }


        if (!Global.instance.IsEnding && LevelDownModelRefreshTime - 50 == 0)
        {
            ResetLevelDownModal();
            status = PlayerStatus.Nomal;
        }

        LevelDownModelRefreshTime++;
        Timer++;
        if (!Global.instance.StartFlag)
        {
            return;
        }

        Vector3 horizental = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
        if (horizental.y >= 0.1 && horizental.x >= 0.1)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 30));
        }
        else if (horizental.y <= -0.1 && horizental.x >= 0.1)
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, -30));
        }
        else
        {
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
        }

    }

    public void ChangeLevelDownModal(LevelSelfDownModel targetModal)
    {
        LevelDownModelRefreshTime = 0;
        ldm = targetModal;
    }

    public void ResetLevelDownModal()
    {
        ldm = LevelSelfDownModel.Nomal;
    }

    public enum LevelSelfDownModel
    {
        Nomal = 0,
        Hited = 15,
        Eat = -15,
    }
    public enum PlayerStatus
    {
        Nomal = 1,
        Muteki = 2,
    }


    public void SetPlayerMuteki()
    {
        status = PlayerStatus.Muteki;
    }
}
